import com.sun.org.apache.xerces.internal.impl.xs.models.XSCMBinOp;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class JedisZset {

    private static void test1(Jedis jedis) {
        System.out.println("-----------------------------");
        System.out.println("zadd 和 zrange");
        jedis.flushAll();

        jedis.zadd("key1", 10, "aaa");
        Map<String, Double> map = new HashMap<>();
        map.put("bbb", 20.0);
        map.put("ccc", 30.0);
        jedis.zadd("key1", map);

        List<String> list = jedis.zrange("key1", 0 ,-1);
        System.out.println(list);
    }

    private static void test2(Jedis jedis) {
        System.out.println("-----------------------------");
        System.out.println("zcard");
        jedis.flushAll();

        jedis.zadd("key1", 10, "aaa");
        jedis.zadd("key1", 20, "bbb");
        jedis.zadd("key1", 30, "ccc");

        long len = jedis.zcard("key1");
        System.out.println(len);
    }

    private static void test3(Jedis jedis) {
        System.out.println("-----------------------------");
        System.out.println("zrem");
        jedis.flushAll();

        jedis.zadd("key1", 10, "aaa");
        jedis.zadd("key1", 20, "bbb");
        jedis.zadd("key1", 30, "ccc");

        long count = jedis.zrem("key1", "aaa", "bbb", "xxx");
        System.out.println(count);
    }

    private static void test4(Jedis jedis) {
        System.out.println("-----------------------------");
        System.out.println("zscore 和 zrank");
        jedis.flushAll();

        jedis.zadd("key1", 10, "aaa");
        jedis.zadd("key1", 20, "bbb");
        jedis.zadd("key1", 30, "ccc");

        Double score = jedis.zscore("key1", "bbb");
        Long rank = jedis.zrank("key1", "bbb");

        System.out.println("score: " + score);
        System.out.println("rank: " + rank);
    }

    public static void main(String[] args) {
        JedisPool jedisPool = new JedisPool("tcp://127.0.0.1:8888");
        try(Jedis jedis = jedisPool.getResource()) {
            test1(jedis);
            test2(jedis);
            test3(jedis);
            test4(jedis);
        }
    }

}
